﻿using Ldt.DataEngine.DataFlowParse.Model;
using LDT.CoreLibrary;
using LdtDataEngin.ModuleDetail.CommonModule.LawDeduce;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.LawElementModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.CommonModule.DataDeduce
{
   public class QutoLawdeByidDuction
    {

        /// <summary>
        /// 中间数据层
        /// </summary>
        public DataContext dataContext { get; set; }
        /// <summary>
        /// 分析数据集合
        /// </summary>
        public List<DataModel> dataModelList = new List<DataModel>();

        /// <summary>
        /// xml帮助类
        /// </summary>
        private XmlUtilCommon xmlUtilCommon;


        /// <summary>
        /// 配置信息
        /// </summary>
        public LawConfigModel lawConfigModel { get; set; }

        public string moudleName { get; set; }

        /// <summary>
        /// 法律法规转换
        /// </summary>
        public void LawDuction()
        {
            xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
            string xmlconfig = "";
            //获取模块配置
            var configModel = dataContext.DataLayerSys.GetConfigModel(moudleName);
            if (configModel != null)
            {
                ConfigModel lawDeductionConfig = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower()== "lawnodeperfect");
                if (lawDeductionConfig!=null && !string.IsNullOrWhiteSpace(lawDeductionConfig.ConfigContent))
                {
                    xmlconfig = lawDeductionConfig.ConfigContent;
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(xmlconfig);
                    lawConfigModel = XmlUtility.DeserializeToObject<LawConfigModel>(xmlconfig);
                }
            }
            if (lawConfigModel != null)
            {
                LawCaseType lawCaseType = lawConfigModel.lawCaseTypeList.FirstOrDefault(a => a.id.IndexOf(dataContext.DataLayerOpt.CaseType) > -1);
                if (lawCaseType != null)
                { 
                    foreach (LawDuctionModel lawDuctionModel in lawConfigModel.lawDuctionModelList)
                    {
                        string lawpath = lawCaseType.parentpath + lawDuctionModel.xmlpath;
                        List<DataModel> dataModelList = XmlUtility.ConditionDataModelList(dataContext.DataLayerOpt.SourdatModleList, "", lawpath);
                        foreach (DataModel dataModel in dataModelList)
                        {
                            if (dataModel != null)
                            {
                                string LawID = dataModel.AttributesList.FirstOrDefault(a => a.Name == "引用法规ID").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                Dictionary<string, string> lawdic = QuoteLawExt.GetLawModelByID(LawID);
                                foreach (LawAttribute lawAttribute in lawDuctionModel.lawAttributesList)
                                {
                                    if (lawdic.ContainsKey(lawAttribute.tablename))
                                    {
                                        DataModelAttSave(lawAttribute.name, lawdic[lawAttribute.tablename], dataModel);
                                    }
                                }
                                List<DataModel> dataModelsProvList = xmlUtilCommon.SubordinateDataModelList(dataContext.DataLayerOpt.SourdatModleList, dataModel);
                                foreach (LawProvDuctionModel lawProvDuction in lawDuctionModel.lawProvDuctionModelList)
                                {
                                    string lawProvpath = lawpath + lawProvDuction.xmlpath;
                                    List<DataModel> lawProvdataModelList = XmlUtility.ConditionDataModelList(dataModelsProvList, dataModel.ID, lawProvDuction.xmlpath);
                                    foreach (DataModel lawProvdataModel in lawProvdataModelList)
                                    {
                                        if (lawProvdataModel != null)
                                        {
                                            string lawProvID = lawProvdataModel.AttributesList.FirstOrDefault(a => a.Name == "引用条ID").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                            Dictionary<string, string> lawProvdatadic = QuoteLawExt.GetLawprovModelByID(lawProvID);
                                            foreach (LawAttribute lawAttribute in lawProvDuction.lawAttributesList)
                                            {
                                                if (lawProvdatadic != null && lawProvdatadic.ContainsKey(lawAttribute.tablename))
                                                {
                                                    DataModelAttSave(lawAttribute.name, lawProvdatadic[lawAttribute.tablename], lawProvdataModel);
                                                }
                                            }
                                            List<DataModel> dataModelsClauseList = xmlUtilCommon.SubordinateDataModelList(dataModelsProvList, lawProvdataModel);
                                            foreach (LawProvClauseDuctionModel lawProvClauseDuctionModel in lawProvDuction.lawProvClauseDuctionModelList)
                                            {
                                                string lawProvClausePath = lawProvpath + lawProvClauseDuctionModel.xmlpath;
                                                List<DataModel> lawProvClausedataModelList = XmlUtility.ConditionDataModelList(dataModelsClauseList, lawProvdataModel.ID, lawProvClauseDuctionModel.xmlpath);
                                                foreach (DataModel lawProvClausedataModel in lawProvClausedataModelList)
                                                {
                                                    if (lawProvClausedataModel != null)
                                                    {
                                                        string lawProvClauseID = lawProvClausedataModel.AttributesList.FirstOrDefault(a => a.Name == "款排序")?.sourceList?.FirstOrDefault(a => a.IsCheck == "1")?.SaveValue;
                                                        Dictionary<string, string> lawProvClausedata = QuoteLawExt.GetLawprovclauseModelByID(lawProvClauseID, lawProvID, "0");
                                                        foreach (LawAttribute lawAttribute in lawProvClauseDuctionModel.lawAttributesList)
                                                        {
                                                            if (lawProvClausedata != null && lawProvClausedata.ContainsKey(lawAttribute.tablename))
                                                            {
                                                                DataModelAttSave(lawAttribute.name, lawProvClausedata[lawAttribute.tablename], lawProvClausedataModel);
                                                            }
                                                        }
                                                        List<DataModel> dataModelsitemList = xmlUtilCommon.SubordinateDataModelList(dataModelsClauseList, lawProvClausedataModel);
                                                        foreach (LawProvClauseItemDuctionModel lawProvClauseitemDuctionModel in lawProvClauseDuctionModel.lawProvClauseItemDuctionModelList)
                                                        {
                                                            string lawProvClauseItemePath = lawProvClausePath + lawProvClauseitemDuctionModel.xmlpath;
                                                            List<DataModel> lawProvClauseItemeDataModelList = XmlUtility.ConditionDataModelList(dataModelsitemList, lawProvClausedataModel.ID, lawProvClauseitemDuctionModel.xmlpath);

                                                            foreach (DataModel lawProvClauseItemeDataModel in lawProvClauseItemeDataModelList)
                                                            {
                                                                if (lawProvClauseItemeDataModel != null)
                                                                {
                                                                    string lawProvClauseItemID = lawProvClauseItemeDataModel.AttributesList.FirstOrDefault(a => a.Name == "项排序").sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                                                    Dictionary<string, string> lawProvClauseItemdata = QuoteLawExt.GetLawprovclauseModelByID(lawProvClauseID, lawProvID, lawProvClauseItemID);
                                                                    foreach (LawAttribute lawAttribute in lawProvClauseitemDuctionModel.lawAttributesList)
                                                                    {
                                                                        if (lawProvClauseItemdata != null && lawProvClauseItemdata.ContainsKey(lawAttribute.tablename))
                                                                        {
                                                                            DataModelAttSave(lawAttribute.name, lawProvClauseItemdata[lawAttribute.tablename], lawProvClauseItemeDataModel);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    #region 保存数据
                    xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
                    XmlDocument xmlDocumentOp = xmlUtilCommon.DataModelByXml(dataContext.DataLayerOpt.SourdatModleList, "");
                    dataContext.DataLayerOpt.OptimizationXml = xmlDocumentOp;
                    #endregion
                }
            }
        }

        /// <summary>
        /// 实体信息保存
        /// </summary>
        /// <param name="attName"></param>
        /// <param name="attValue"></param>
        public void DataModelAttSave(string attName, string attValue, DataModel dataModel)
        {
            var attmodel = dataModel.AttributesList.FirstOrDefault(a => a.Name == attName);
            if (string.IsNullOrEmpty(attValue))
            {
                return;
            }
            if (attmodel != null)
            {
                attmodel.sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue = attValue;
            }
            else
            {
                AttributeInfo attribute = new AttributeInfo();
                attribute.Name = attName;
                Source source = new Source();
                source.IsCheck = "1";
                source.SaveValue = attValue;
                source.ExtValue = attValue;
                source.SourceCode = dataContext.DataLayerAns.SourceCode;
                source.SourceID = dataContext.DataLayerAns.SF_ID;
                source.SourceFLID = dataContext.DataLayerAns.FL_ID;
                attribute.sourceList = new List<Source>();
                attribute.sourceList.Add(source);
                if (dataModel.AttributesList == null)
                {
                    dataModel.AttributesList = new List<AttributeInfo>();
                }
                dataModel.AttributesList.Add(attribute);
            }

        }

    }
}
